// packageF/crowd_activity/index/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    listData: [],
    activeChild: 0,
    crowdfund_diy_name: '',
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let {fontSizeSetting} = wx.getSystemInfoSync();
    this.setData({
      fontSizeSetting:`${fontSizeSetting}px`
    })
  },
  init() {
    this.setData({
      listData: [],
      isLoadMore: [],
      page: 1,
      total_page: 0,
      activeChild: 0
    })
  },
  changeTap(e) {
    this.setData({
      active: e.detail.index
    })
    this.init();
    this.getData();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData();
    let crowdfund_diy_name = wx.getStorageSync('yz_basic_info')?.plugin_setting?.crowdfund_diy_name;
    this.setData({
      crowdfund_diy_name: crowdfund_diy_name,
    })
    wx.setNavigationBarTitle({
      title: crowdfund_diy_name,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onShareTimeline() {},
  getChildIndex(e) {
    this.init();
    this.data.activeChild = e.detail;
    this.getData();
  },
  getData() {
    let urlStr;
    let json = {
      page: this.data.page
    };
    console.log(this.data.active, 'ddddddddddddddddddd')
    if (this.data.active != 1) {
      switch (this.data.activeChild) {
        case 1:
          json.status = 0;
          break;
        case 2:
          json.status = 1;
          break;
        case 3:
          json.status = 2;
          break;
        default:
          json.status = "";
      }
    }
    if (this.data.active == 0) {
      urlStr = "plugin.crowdfunding.frontend.activity-member.crowdfunding"
    } else if (this.data.active == 1) {
      urlStr = "plugin.crowdfunding.frontend.activity-member.selfPaid"
      json.status = ''
    } else {
      urlStr = "plugin.crowdfunding.frontend.activity-member.support"
    }
    urlStr = app.getNetAddresss(urlStr);
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.data.isLoadMore = true;
          this.data.total_page = res.data.last_page;
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
          this.setData({
            listData: res.data.data
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMore() {
    let urlStr = "";
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let json = {
        page: this.data.page
      };
      if (this.data.active != 1) {
        switch (this.data.activeChild) {
          case 1:
            json.status = 0;
            break;
          case 2:
            json.status = 1;
            break;
          case 3:
            json.status = 2;
            break;
          default:
            json.status = "";
        }
      }
      if (this.data.active == 0) {
        urlStr = "plugin.crowdfunding.frontend.activity-member.crowdfunding"
      } else if (this.data.active == 1) {
        urlStr = "plugin.crowdfunding.frontend.activity-member.selfPaid"
        json.status = ""
      } else {
        urlStr = "plugin.crowdfunding.frontend.activity-member.support"
      }
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              listData: this.data.listData.concat(res.data.data)
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  }
})